<template>
  <div v-loading="loading" @wheel="handelWheel" ref="container" class="home-container">
    <!-- 背景图片 -->
    <ul @transitionend="handelTransitionEnd" class="carousel-container" :style="{ marginTop }">
      <li v-for="item in data" :key="item.id">
        <Carouselitem :carousel="item" />
      </li>
    </ul>
    <!-- 背景图片结束 -->
    <!-- 上下小箭头 -->
    <div @click="switchTo(index - 1)" v-show="index >= 1" class="icon icon-up">
      <Icon type="arrowUp" />
    </div>
    <div @click="switchTo(index + 1)" v-show="index < data.length - 1" class="icon icon-down">
      <Icon type="arrowDown" />
    </div>
    <!-- 上下小箭头结束 -->
    <!-- 指示器 -->
    <ul class="indicator">
      <li @click="switchTo(i)" :class="{ active: index === i }" v-for="(item, i) in data" :key="item.id"></li>
    </ul>
    <!-- 指示器结束 -->
  </div>
</template>

<script>
import Carouselitem from './Carouselitem.vue';
import Icon from '@/components/Icon';
import { mapState } from 'vuex';

export default {
  components: {
    Carouselitem,
    Icon,
  },
  data() {
    return {
      index: 0, // 当前显示的是第几张轮播图
      containerHeight: 0, // 整个容器的高度
      switching: false, // 是否正在切换中
    }
  },
  created(){
    this.$store.dispatch("banner/fetchBanner");
  },
  methods: {
    switchTo(i) {
      this.index = i;
      this.switching = false;
    },
    handelWheel(e) {
      if (this.switching) { return; }
      if (e.deltaY < -5 && this.index > 0) {
        // 向上滚动
        this.index--;
        this.switching = true;
      }
      else if (e.deltaY > 5 && this.index < this.data.length - 1) {
        // 向下滚动
        this.index++;
        this.switching = true;
      }
    },
    handelTransitionEnd() {
      // 换页结束
      this.switching = false;
    },
    handelResize() {
      this.containerHeight = this.$refs.container.clientHeight;
    }
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    // 当窗口大小变化，重新获取窗口宽高
    window.addEventListener('resize', this.handelResize)
  },
  destroyed() {
    // 到其他页面的时候移除window监听事件
    window.removeEventListener('resize', this.handelResize)
  },
  computed: {
    ...mapState("banner",["loading","data"]),
    marginTop() { // 计算容器的高度值
      return -this.index * this.containerHeight + 'px';
    }
  },
}
</script>

<style lang="less" scoped>
@import url(~@/styles/mixin.less);
@import url(~@/styles/var.less);
@gap: 25px;

.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  // background: @dark;
  overflow: hidden;

  ul {
    margin: 0;
    list-style: none;
    padding: 0;
  }

  .carousel-container {
    .fill();
    transition: .5s;

    li {
      .fill();
    }
  }

  .icon {
    .self-center();
    font-size: 30px;
    color: @gray;
    cursor: pointer;
    transform: translateX(-50%);

    &.icon-up {
      top: @gap;
      animation: jump-up 2s infinite;
    }

    &.icon-down {
      top: auto;
      bottom: @gap;
      animation: jump-down 2s infinite;
    }

    @jump: 5px;

    @keyframes jump-up {
      0% {
        transform: translate(-50%, @jump);
      }

      50% {
        transform: translate(-50%, -@jump);
      }

      100% {
        transform: translate(-50%, @jump);
      }
    }

    @keyframes jump-down {
      0% {
        transform: translate(-50%, -@jump);
      }

      50% {
        transform: translate(-50%, @jump);
      }

      100% {
        transform: translate(-50%, -@jump);
      }
    }
  }

  .indicator {
    .self-center();
    transform: translateY(-50%);
    left: auto;
    right: 20px;

    li {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      background: #333;
      cursor: pointer;
      margin-bottom: 10px;
      border: 1px solid @f;
      box-sizing: border-box;
      transition: .5s;

      &.active {
        background: @f;
      }
    }
  }

}
</style>